<template>
    <div class="wrapper">
        <!-- 顶部 -->
        <div class="top">在线商城系统</div>

        <!-- 主体 -->
        <div class="main">
            <div class="left">
                <img src="../assets/register.png" style="width: 100%;">
            </div>
            <div class="right">
                <!-- 标题 -->
                <div class="title">欢迎注册</div>
                <!-- 表单 -->
                <el-form :model="user" :rules="rules" ref="userForm">
                    <el-form-item prop="userName">
                        <el-input v-model="user.userName" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input v-model="user.password" placeholder="请输入密码" prefix-icon="el-icon-lock"
                            show-password></el-input>
                    </el-form-item>
                    <el-form-item prop="confirmPassword">
                        <el-input v-model="user.confirmPassword" placeholder="请输入确认密码" prefix-icon="el-icon-lock"
                            show-password></el-input>
                    </el-form-item>
                    <el-form-item prop="nickname">
                        <el-input v-model="user.nickname" placeholder="请输入昵称"
                            prefix-icon="el-icon-user-solid"></el-input>
                    </el-form-item>
                    <el-form-item prop="email">
                        <el-input v-model="user.email" placeholder="请输入邮箱" prefix-icon="el-icon-message"></el-input>
                    </el-form-item>
                    <el-form-item prop="phone">
                        <el-input v-model="user.phone" placeholder="请输入电话" prefix-icon="el-icon-phone"></el-input>
                    </el-form-item>
                    <el-form-item prop="address">
                        <el-input v-model="user.address" placeholder="请输入地址" prefix-icon="el-icon-s-home"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="register">注册</el-button>
                        <el-button type="warning" @click="$router.push('/login')">返回登录</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            user: {},
            rules: {
                userName: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
                ],
                confirmPassword: [
                    { required: true, message: '请输入确认密码', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
                ],
                nickname: [
                    { required: true, message: '请输入昵称', trigger: 'blur' },
                ],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/, message: '邮箱格式不正确', trigger: 'blur' }
                ],
                phone: [
                    { required: true, message: '请输入电话', trigger: 'blur' },
                    { pattern: /^1\d{10}$/, message: '电话格式不正确', trigger: 'blur' }
                ],
                address: [
                    { required: true, message: '请输入地址', trigger: 'blur' },
                ],
            },
        }
    },
    mounted() {
    },
    methods: {
        register() {
            this.$refs.userForm.validate(valid => {
                if (!valid) return

                // 判断两次输入的密码是否一致
                if (this.user.password !== this.user.confirmPassword) {
                    return this.$message.error('两次输入的密码不一致')
                }

                // 发送请求
                this.request.post('/user/register', this.user).then(res => {
                    if (res.status !== 200) {
                        return this.$message.error(res.msg)
                    }
                    this.$message.success('注册成功')
                    this.$router.push('/login')
                })
            })
        }
    }
}
</script>

<style lang="less" scoped>
.wrapper {
    height: 100vh;
    background-image: linear-gradient(to bottom right, #446de2, #85caf6);

    /* 顶部 */
    .top {
        height: 60px;
        line-height: 60px;
        font-size: 20px;
        color: white;
        background-color: #3b5fb7;
        padding-left: 50px;
    }

    /* 主体 */
    .main {
        width: 50%;
        margin: 150px auto;
        background-color: white;
        border-radius: 10px;
        display: flex;

        /* 左侧 */
        .left {
            width: 40%;
        }

        /* 右侧 */
        .right {
            width: 350px;
            margin-top: 30px;
            margin-left: 50px;

            .title {
                text-align: center;
                font-size: 24px;
                font-weight: bold;
                letter-spacing: 6px;
                margin: 20px 0;
            }
        }
    }
}
</style>